package bean;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.imageio.ImageIO;
import javax.media.jai.JAI;
import javax.media.jai.RenderableOp;
import javax.media.jai.RenderedOp;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

public class BoardDTO {
	private BoardVO vo = null;
	private MemberVO vo2 = null;
	private Connection conn = null;

	int listSize = 10; // 페이지당 표시되는 행수
	int totSize = 0; // 전체 게시물의 갯수
	int nowPage = 3; // 현재 페이지
	int pageNo = 0;  // 페이지 보여주는거
	
	public BoardDTO() {
		conn = new MyOracle().getConn();
	}
	
	public BoardVO getVo() {
		return vo;
	}
	
	public boolean delete(){
		boolean flag = false;
		
		String sql = "delete from board_ where b_rno=? and b_mid=?";
		
		try {
			PreparedStatement psmt = conn.prepareStatement(sql);
			psmt.setInt(1, vo.getB_rno());
			psmt.setString(2, vo.getM_id());
			
			int cnt = psmt.executeUpdate();
			
			if(cnt>0) {
				flag = true;
			}
			
			psmt.close();
			conn.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return flag;
	}
	
	public boolean insert() {
		boolean flag = false;
		try {
			String sql = "insert into board_(b_rno, m_id, b_text,b_date) values(seq_board__rno.nextval,?,?,sysdate)";
			PreparedStatement psmt = conn.prepareStatement(sql);
			psmt.setString(1, vo.getM_id());
			System.out.println( "mid = " + vo.getM_id() );
			psmt.setString(2, vo.getB_text());
			 
			//vo.getB_text()
			 
			int affectedRow = psmt.executeUpdate();
			if (affectedRow > 0) {
				flag = true;
			}
			psmt.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public void setVO(BoardVO vo) {
		this.vo = vo; // multipartrequest를 여기서 생성한다.
	}

	public void page(){
		String sql = "select count(*) cnt from board_ "
				+ "where m_id like ?";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString( 1, "%"+vo.getFind()+"%" );
			
			ResultSet rs = pst.executeQuery();
			
			if(rs.next()){
				
				nowPage = vo.getNowPage();
				totSize = rs.getInt("cnt");
				pageNo = totSize-nowPage;
				
				System.out.println("nowPage : " + nowPage);
				System.out.println("totSize : " + totSize);
				System.out.println("pageNo : " + pageNo);
			}
		} 
		catch (Exception e) {
			
		}
	}
	
	public ArrayList<BoardVO> select(){
		page();
		ArrayList<BoardVO> al = new ArrayList<>();
		System.out.println("SQL시작");
		String sql = "select b.b_rno, b.b_text, b.b_date, b.m_id, m.m_gender, m.m_age, m_local, m_job "
					+"  from board_ b, member_ m  "
					+"  where b.m_id = m.m_id and b_rno between ? and ?"
					+"order by b_rno desc";
		try{
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setInt(1, pageNo);
			pst.setInt(2, (totSize + 1));
			
			System.out.println( "find : " + vo.getFind() );
			System.out.println( "pageNo : " + pageNo );
			System.out.println( "totSize : " + totSize );
			
			ResultSet rs = pst.executeQuery();
			int cnt = 0;
			while( rs.next() ){
				cnt++;
				BoardVO v = new BoardVO();
				MemberVO v2 = new MemberVO();	
					v.setB_rno(rs.getInt("b_rno"));
					v.setB_text(rs.getString("b_text"));
					v.setB_date(rs.getString("b_date"));
					v.setM_id(rs.getString("m_id"));
				al.add(v);
			}
			System.out.println(cnt);
			rs.close();
			pst.close();
			
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		return al;
	}
	
	public ArrayList<MemberVO> select2(){
		page();
		ArrayList<MemberVO> al2 = new ArrayList<>();
		System.out.println("SQL시작");
		String sql = "select b.b_rno, b.b_text, b.b_date, b.m_id, m.m_gender, m.m_age, m_local, m_job "
					+"  from board_ b, member_ m  "
					+"  where b.m_id = m.m_id and b_rno between ? and ?+1"
					+"order by b_rno desc";
		try{
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setInt(1, pageNo);
			pst.setInt(2, totSize);
			
			System.out.println( "find : " + vo.getFind() );
			System.out.println( "pageNo : " + pageNo );
			System.out.println( "totSize : " + totSize );
			
			ResultSet rs = pst.executeQuery();
			int cnt = 0;
			while( rs.next() ){
				cnt++;
				BoardVO v = new BoardVO();
				MemberVO v2 = new MemberVO();	
					v2.setM_gender( rs.getString("m_age"));
					v2.setM_age(rs.getInt("m_age"));
					v2.setM_local( rs.getString("m_local"));
					v2.setM_job( rs.getString("m_job"));
				al2.add(v2);
			}
			System.out.println(cnt);
			rs.close();
			pst.close();
			
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		return al2;
	}
	
	
	public BoardVO selectOne(){
		BoardVO v = new BoardVO();
		String sql = "select * from board_ where m_id = ?";

		try {
			PreparedStatement psmt = conn.prepareStatement(sql);
			psmt.setInt(1, vo.getB_rno());
			ResultSet rs = psmt.executeQuery();

			if (rs.next()) {
				 v.setM_id(rs.getString("m_id"));
				 v.setB_rno(rs.getInt("b_rno"));
				 v.setB_text(rs.getString("b_text"));
				 v.setB_date(rs.getString("b_date"));
			}
			rs.close();
			psmt.close();
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return v;
	}
	
	public MemberVO getVo2() {
		return vo2;
	}

	public void setVo2(MemberVO vo2) {
		this.vo2 = vo2;
	}

	public void setVo(BoardVO vo) {
		this.vo = vo;
	}

	public int getListSize() {
		return listSize;
	}

	public void setListSize(int listSize) {
		this.listSize = listSize;
	}

	public int getNowPage() {
		return nowPage;
	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}
	
	public int getTotSize() {
		return totSize;
	}
	
	public void setTotSize(int totSize) {
		this.totSize = totSize;
	}
}